import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;

public class ChatServer {
	public static final int LOCAL_UDP_PORT = 16403;
	public static final int PEER_UDP_PORT = 16404;
	public static final int TCP_PORT = 9800;
	
	public final static int bufsize = 4092;
	private static int count = 0;

	// private static final int peerport = 5100;

	private static UdpHandler2 udphandler;
	private static SIPEngine sipEngine;

	
	
	public static void main(String[] args) {
		int port = TCP_PORT;
		udphandler = new UdpHandler2();
		udphandler.start();
		
		
		//sipEngine = new SIPEngine(TCP_PORT);
		//sipEngine.start();
		System.out.println("Server is Ready");
		
		Socket TCPSocket = null;
		
		ServerSocket serverSocket = null;
		try {
			serverSocket = new ServerSocket(TCP_PORT);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		byte[] buf = new byte[4092];
		
		while (true) {
			try {
				TCPSocket = serverSocket.accept();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if(!UdpHandler2.arraylist.contains(TCPSocket.getInetAddress())){/////////////////////change back
				System.out.println("a client is added!!");
				UdpHandler2.arraylist.add(TCPSocket.getInetAddress());/////////////////////////change back
				DatagramPacket packet = new DatagramPacket(buf,0,buf.length, TCPSocket.getInetAddress(),UdpHandler2.PEER_UDP_PORT);
				UdpHandler2.packetlist.add(packet);
				System.out.println("flag");
			}
			
		}
		
		
		
	}
}
